package guoanmaker.provider.business.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import guoanmaker.provider.business.modelnobind.ProvideOrderFormForOrder;
import guoanmaker.provider.business.modelnobind.SpaceReserve;
/**
 * 
* @ClassName: ProvideSpaceReserveDao 
* @Description: 预约空间Dao
* @author Yvonne_MGL
* @date 2017年8月15日 上午10:38:11 
*  
 */
@Repository
public class ProvideSpaceReserveDao {
	@PersistenceContext
	EntityManager em;
	/**
	 * 
	* @Description: 根据条件查询预约空间 
	* @param beginTime 参观起始时间
	* @param endTime 参观结束时间
	* @param likeStr 关键字：模糊查询 联系人，空间名，备注信息
	* @param state 0为未预约  1为已预约
	* @param pageNumber 页数
	* @param pageSize 每页条数
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月15日 上午10:57:59     
	* @return Map    
	* @throws 
	 */
	public Map findByProperties(String beginTime,String endTime,String likeStr,String state,String businessid, Integer pageNumber, Integer pageSize,String userid){
		String jpql ="select new guoanmaker.provider.business.modelnobind.SpaceReserve(s.id,s.createTime,s.reserveTime,s.contactPerson,"
				+ "s.contactNumber,s.reserveAmount,s.remark,s.state,r.roomName) from ProvideSpaceReserve s left join s.user u left join s.room r left join r.business b where 1=1";
		if(beginTime!=null&&!beginTime.equals("")){
			jpql+=" and s.reserveTime>='"+beginTime+"'";
		}
		if(endTime!=null&&!endTime.equals("")){
			jpql+=" and s.reserveTime<='"+endTime+"'";
		}
		if(likeStr!=null&&!likeStr.equals("")){
			jpql+=" and(s.contactPerson like '%"+likeStr+"%' or s.remark like '%"+likeStr+"%' or r.roomName like '%"+likeStr+"%')";
		}
		if(state!=null&&!state.equals("")){
			jpql+=" and s.state='"+state+"'";
		}
		if(businessid!=null&&!businessid.equals("")){
			jpql+=" and b.id='"+businessid+"'";
		}
		if(userid!=null&&!userid.equals("")){
			jpql+=" and u.id='"+userid+"'";
		}
		jpql+=" order by s.createTime desc";
		if(pageNumber==null){
			pageNumber=0;
		}
		if (pageSize==null) {
			pageSize=10;
		}
		Query query = em.createQuery(jpql);
		List<SpaceReserve> list = query.getResultList();
		Map map=new HashMap();
		map.put("totle", list.size());
		query.setFirstResult(pageNumber*pageSize); // 序号从0 开始
		query.setMaxResults(pageSize);
		List<SpaceReserve> result = query.getResultList();
		map.put("result", result);
		return map;
	}
	
	/**
	 * 
	* @Description: 根据id查询预约参观详情 
	* @param id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年10月14日 上午11:13:00     
	* @return SpaceReserve    
	* @throws 
	 */
	public SpaceReserve findSpaceReserveById(String id){
		String jpql ="select new guoanmaker.provider.business.modelnobind.SpaceReserve(s.id,s.createTime,s.reserveTime,s.contactPerson,"
				+ "s.contactNumber,s.reserveAmount,s.remark,s.state,r.roomName) from ProvideSpaceReserve s  left join s.room r  where s.id='"+id+"'";
		Query query = em.createQuery(jpql);
		List<SpaceReserve> list = query.getResultList();
		SpaceReserve spaceReserve=null;
		if(list!=null&&list.size()>0){
			spaceReserve=list.get(0);
		}
		return spaceReserve;
	}
	

}
